// pages/scart/scart.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartlists: [],
    numtotal: 0,
    pricetotal: 0,
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    wx.getStorage({
      key: 'carlists',
      success: function(res) {
        console.log(res.data)
        that.setData({
          cartlists: res.data
        })
        that.sum()
      },
    })
  },

  //减少数量
  reduce: function(e) {
    let that = this
    wx.getStorage({
      key: 'carlists',
      success: function(res) {
        res.data.forEach((item, index) => {
          if (index == e.currentTarget.dataset.index) {
            item.num = item.num - 1
            if (item.num < 1) {
              item.num = 1
            }
            that.setData({
              cartlists: res.data
            })
            wx.getStorage({
              key: 'shopsnum',
              success: function(re) {
                re.data[index] = item.num
                wx.setStorage({
                  key: 'shopsnum',
                  data: re.data,
                })
              },
            })
          }
        })
        that.sum()
        wx.setStorage({
          key: 'carlists',
          data: res.data,
        })

      },
    })
  },
  //增加数量
  add: function(e) {
    let that = this
    wx.getStorage({
      key: 'carlists',
      success: function(res) {
        res.data.forEach((item, index) => {
          if (index == e.currentTarget.dataset.index) {
            item.num = item.num + 1
            if (item.num > 10) {
              item.num = 10
              wx.showToast({
                title: '限购10件',
                icon: 'none',
                mask: true
              })
            }
            that.setData({
              cartlists: res.data
            })
            wx.getStorage({
              key: 'shopsnum',
              success: function (re) {
                re.data[index] = item.num
                wx.setStorage({
                  key: 'shopsnum',
                  data: re.data,
                })
              },
            })
          }
        })
        that.sum()
        wx.setStorage({
          key: 'carlists',
          data: res.data,
        })

      },
    })
  },

  //删除购物车列表信息
  dellist: function(e) {
    let that = this
    wx.getStorage({
      key: 'carlists',
      success: function(res) {
        res.data.forEach((item, index) => {
          if (item.id == e.currentTarget.dataset.id && item.type == e.currentTarget.dataset.type) {
            res.data.splice(index, 1)
            wx.getStorage({
              key: 'shopsnum',
              success: function(res) {
                res.data.splice(index, 1)
                wx.setStorage({
                  key: 'shopsnum',
                  data: res.data,
                })
              }
            })
          }
        })
        that.setData({
          cartlists: res.data
        })
        wx.setStorage({
          key: 'carlists',
          data: res.data,
        })
        that.sum()
      },
    })
  },
  //购物车没有商品时，点击去购物回到首页
  goshopping: function() {
    wx.switchTab({
      url: '/pages/home/home'
    })
  },


  //价格和数量数组求和
  sum: function() {
    let that = this
    let numSum = []
    let priceSum = []
    that.data.cartlists.forEach((item, index) => {
      numSum.push(item.num)
      priceSum.push(item.num * item.price)
    })
    if (numSum.length != 0 && priceSum.length != 0) {
      that.setData({
        numtotal: numSum.reduce((item, index) => {
          return item + index
        }),
        pricetotal: priceSum.reduce((item, index) => {
          return item + index
        }).toFixed(2)
      })
    }
  },

  //点击提交订单，跳转确认订单页
  confirmorder: function () {
    if (app.globalData.userInfo == null) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        confirmText: '确定',
        cancelText: '取消',
        success: function (res) {
          if (res.confirm) {
            wx.getStorage({
              key: 'accreditInfo',
              success: function(res) {
                console.log(6666)
                wx.navigateTo({
                  url: '/pages/setting/setting',
                })
              },
              fail:function(){
                console.log(7777)
                wx.switchTab({
                  url: '/pages/personal/personal',
                })
              }
            })
          } else if (res.cancel) {
            return;
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/confirmorder/confirmorder?buynow=0',
      })
    }
    // wx.navigateTo({
    //   url: '/pages/confirmorder/confirmorder?buynow=0',
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})